<?php

namespace app\beautiful\controller\cashier;
include root_path() . 'extend/phpexcel/PHPExcel.php';

use app\BaseController;
use app\beautiful\model\appointment\PositionModel;
use app\beautiful\model\appointment\TechnicianModel;
use app\beautiful\model\LevelModel;
use app\beautiful\model\member\MemberBalanceModel;
use app\beautiful\model\member\MemberModel;
use think\Db;

class Position extends BaseController
{
    protected $name = '岗位';

    public function getPosition()
    {
        $position_model = new PositionModel();
        $where = [
            ['shop_id', '=', $this->request->shopId],
        ];
        $name = $this->request->param('name/s');
        if (!empty($name)) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        $is_show = $this->request->param('is_show');

        if ($is_show != '') {
            $where[] = ['is_show', '=', $is_show];
        }
        $list = $position_model->where($where)->select();
        return $this->success($list);
    }

    public function excel()
    {
        // Excel文件路径
//        $file = '/TEST.xlsx';
        $file = root_path() . '/public/TEST.xlsx';

        // 创建PHPExcel对象
        $excel = \PHPExcel_IOFactory::load($file);
        // 获取第一个工作表
        $sheet = $excel->getActiveSheet();
        $endCol = $sheet->getHighestColumn();

// 指定起始行数和结束行数
        $startRow = 6;
        $endRow = $sheet->getHighestRow();
//        $endRow = 10;

//        获取会员等级信息
        $levelmode = LevelModel::where([
            ['shop_id','=',3]
        ])->field('discount,level_id')->select()->toArray();
        $levellist = [];
        foreach ($levelmode as $value){
            $levellist[$value['discount']] = $value['level_id'];
        }
// 循环读取指定行范围内的数据
        for ($row = $startRow; $row <= $endRow; $row++) {
            $rowData = $sheet->rangeToArray('A' . $row . ':' . $endCol . $row, NULL, TRUE, FALSE);
            preg_match('/[\d.]+/', $rowData[0][2], $matches); // 使用正则表达式进行匹配
            if (isset($matches[0])) {
                $discount = $matches[0];
            } else {
                $discount = null;
            }
//            获取用户所属数据
            $userrow = array(
                'nick_name' => $rowData[0][1],
                'sex' => $rowData[0][3] == '女' ? 2 : 1,
                'mobile' => $rowData[0][6],
                'balance'=> $rowData[0][8],
                'remain_balance'=> $rowData[0][9],
                'level_id'=>$discount!=null && array_key_exists($discount,$levellist) ? $levellist[$discount] : 0
            );

            \think\facade\Db::startTrans();
            try {
                $user = MemberModel::where([['mobile','=',$userrow['mobile']]])->find();
                if($user==null){
                    $user = new MemberModel();
                    $user->mobile = $userrow['mobile'];
                    $user->shop_id = 3;
                    $user->sex = $userrow['sex'];
                    $user->nick_name = $userrow['nick_name'];
                    $user->level_id = $userrow['level_id'];
                    $user->save();

                    $ban= new MemberBalanceModel();
                    $ban->shop_id = 3;
                    $ban->member_id = $user->member_id;
                    $ban->balance = $userrow['balance'];
                    $ban->remain_balance = $userrow['remain_balance'];
                    $ban->valid_bg_time = time();
                    $ban->save();
                }
                // 提交事务
                \think\facade\Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                \think\facade\Db::rollback();
            }
            // 获取行数据

        }

    }
}
